home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // FileView.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include <afxcview.h>
- #include "Resource.h"
- #include "FileView.h"
-
- IMPLEMENT_DYNCREATE (CFileView, CListView)
-
- BEGIN_MESSAGE_MAP (CFileView, CListView)
- ON_WM_CREATE ()
- ON_WM_DESTROY ()
- ON_COMMAND_RANGE (ID_VIEW_LARGE_ICON, ID_VIEW_REPORT, OnChangeView)
- ON_UPDATE_COMMAND_UI_RANGE (ID_VIEW_LARGE_ICON, ID_VIEW_REPORT,
- OnUpdateViewUI)
- ON_NOTIFY_REFLECT (LVN_GETDISPINFO, OnGetDispInfo)
- ON_NOTIFY_REFLECT (LVN_COLUMNCLICK, OnColumnClick)
- END_MESSAGE_MAP ()
-
- const DWORD CFileView::m_dwStyleList[4] = {
- LVS_ICON,
- LVS_SMALLICON,
- LVS_LIST,
- LVS_REPORT
- };
-
- BOOL CFileView::PreCreateWindow (CREATESTRUCT& cs)
- {
- if (!CListView::PreCreateWindow (cs))
- return FALSE;
-
- cs.style &= ~LVS_TYPEMASK;
- cs.style |= LVS_REPORT;
- return TRUE;
- }
-
- int CFileView::OnCreate (LPCREATESTRUCT lpcs)
- {
- if (!CListView::OnCreate (lpcs) == -1)
- return -1;
-
- m_imglLarge.Create (IDR_LARGEDOC, 32, 1, RGB (255, 0, 255));
- m_imglSmall.Create (IDR_SMALLDOC, 16, 1, RGB (255, 0, 255));
-
- GetListCtrl ().SetImageList (&m_imglLarge, LVSIL_NORMAL);
- GetListCtrl ().SetImageList (&m_imglSmall, LVSIL_SMALL);
-
- GetListCtrl ().InsertColumn (0, "File Name", LVCFMT_LEFT, 192);
- GetListCtrl ().InsertColumn (1, "Size", LVCFMT_RIGHT, 96);
- GetListCtrl ().InsertColumn (2, "Last Modified", LVCFMT_CENTER, 128);
- return 0;
- }
-
- void CFileView::OnDestroy ()
- {
- FreeItemMemory ();
- CListView::OnDestroy ();
- }
-
- void CFileView::OnUpdate (CView* pView, LPARAM lHint, CObject* pHint)
- {
- if (lHint != NULL) {
- FreeItemMemory ();
- GetListCtrl ().DeleteAllItems ();
- InitList ((LPCTSTR) lHint);
- return;
- }
- CListView::OnUpdate (pView, lHint, pHint);
- }
-
- void CFileView::OnChangeView (UINT nID)
- {
- ModifyStyle (LVS_TYPEMASK,
- m_dwStyleList[nID - ID_VIEW_LARGE_ICON]);
- }
-
- void CFileView::OnUpdateViewUI (CCmdUI* pCmdUI)
- {
- DWORD dwCurrentStyle = GetStyle () & LVS_TYPEMASK;
- pCmdUI->SetCheck (dwCurrentStyle ==
- m_dwStyleList[pCmdUI->m_nID - ID_VIEW_LARGE_ICON]);
- }
-
- void CFileView::OnGetDispInfo (NMHDR* pnmh, LRESULT* pResult)
- {
- CString string;
- LV_DISPINFO* plvdi = (LV_DISPINFO*) pnmh;
-
- if (plvdi->item.mask & LVIF_TEXT) {
- ITEMINFO* pItem = (ITEMINFO*) plvdi->item.lParam;
-
- switch (plvdi->item.iSubItem) {
-
- case 0: // File name
- ::lstrcpy (plvdi->item.pszText, (LPCTSTR) pItem->strFileName);
- break;
-
- case 1: // File size
- string.Format ("%u", pItem->nFileSizeLow);
- ::lstrcpy (plvdi->item.pszText, (LPCTSTR) string);
- break;
-
- case 2: // Date and time
- CTime time (pItem->ftLastWriteTime);
-
- BOOL pm = FALSE;
- int nHour = time.GetHour ();
- if (nHour == 0)
- nHour = 12;
- else if (nHour == 12)
- pm = TRUE;
- else if (nHour > 12) {
- nHour -= 12;
- pm = TRUE;
- }
-
- string.Format ("%d/%0.2d/%0.2d (%d:%0.2d%c)",
- time.GetMonth (), time.GetDay (), time.GetYear () % 100,
- nHour, time.GetMinute (), pm ? 'p' : 'a');
- ::lstrcpy (plvdi->item.pszText, (LPCTSTR) string);
- break;
- }
- }
- }
-
- void CFileView::OnColumnClick (NMHDR* pnmh, LRESULT* pResult)
- {
- NM_LISTVIEW* pnmlv = (NM_LISTVIEW*) pnmh;
- GetListCtrl ().SortItems (CompareFunc, pnmlv->iSubItem);
- }
-
- int CALLBACK CFileView::CompareFunc (LPARAM lParam1, LPARAM lParam2,
- LPARAM lParamSort)
- {
- ITEMINFO* pItem1 = (ITEMINFO*) lParam1;
- ITEMINFO* pItem2 = (ITEMINFO*) lParam2;
- int nResult;
-
- switch (lParamSort) {
-
- case 0: // File name
- nResult = pItem1->strFileName.CompareNoCase (pItem2->strFileName);
- break;
-
- case 1: // File size
- nResult = pItem1->nFileSizeLow - pItem2->nFileSizeLow;
- break;
-
- case 2: // Date and time
- nResult = ::CompareFileTime (&pItem1->ftLastWriteTime,
- &pItem2->ftLastWriteTime);
- break;
- }
- return nResult;
- }
-
- int CFileView::InitList (LPCTSTR pszPath)
- {
- CString strPath = pszPath;
- if (strPath.Right (1) != "\\")
- strPath += "\\";
- strPath += "*.*";
-
- HANDLE hFind;
- WIN32_FIND_DATA fd;
- int nCount = 0;
-
- if ((hFind = ::FindFirstFile ((LPCTSTR) strPath, &fd)) !=
- INVALID_HANDLE_VALUE) {
-
- if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
- AddItem (nCount, &fd);
- nCount++;
- }
-
- while (::FindNextFile (hFind, &fd)) {
- if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
- if (!AddItem (nCount, &fd))
- break;
- nCount++;
- }
- CloseHandle (hFind);
- }
- return nCount;
- }
-
- BOOL CFileView::AddItem (int nIndex, WIN32_FIND_DATA* pfd)
- {
- ITEMINFO* pItem;
- try {
- pItem = new ITEMINFO;
- }
- catch (CMemoryException* e) {
- e->Delete ();
- return FALSE;
- }
-
- pItem->strFileName = pfd->cFileName;
- pItem->nFileSizeLow = pfd->nFileSizeLow;
- pItem->ftLastWriteTime = pfd->ftLastWriteTime;
-
- LV_ITEM lvi;
- lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
- lvi.iItem = nIndex;
- lvi.iSubItem = 0;
- lvi.iImage = 0;
- lvi.pszText = LPSTR_TEXTCALLBACK;
- lvi.lParam = (LPARAM) pItem;
-
- if (GetListCtrl ().InsertItem (&lvi) == -1)
- return FALSE;
-
- return TRUE;
- }
-
- void CFileView::FreeItemMemory ()
- {
- int nCount = GetListCtrl ().GetItemCount ();
- if (nCount) {
- for (int i=0; i<nCount; i++)
- delete (ITEMINFO*) GetListCtrl ().GetItemData (i);
- }
- }
-